use crate::dto::*;
use crate::entity::*;
use crate::RB;
use hypers::prelude::{hypers_openapi, openapi, OpenApi, Responder, Response};
use rbatis::plugin::page::PageRequest;
use rbatis::rbdc::datetime::DateTime;

pub struct Member;

#[openapi(
    prefix = "/api",
    components(schemas(MemberSaveReq, MemberDeleteReq, MemberUpdateReq,MemberListReq))
)]
impl Member {
    #[post("member_save",tag = "添加会员信息",request_body = MemberSaveReq)]
    async fn member_save(item: MemberSaveReq) -> impl Responder {
        log::info!("member_save params: {:?}", &item);
        let member = InterviewMember {
            id: None,
            phone: item.phone,
            name: item.name,
            password: item.password,
            level: item.level,
            create_time: Some(DateTime::now()),
            update_time: Some(DateTime::now()),
        };
        let result = InterviewMember::insert(&mut RB.clone(), &member).await;
        handle_result(result)
    }

    #[post("member_delete",tag = "删除会员信息",request_body = MemberDeleteReq)]
    async fn member_delete(item: MemberDeleteReq) -> impl Responder {
        log::info!("member_delete params: {:?}", &item);
        let result = InterviewMember::delete_in_column(&mut RB.clone(), "id", &item.ids).await;
        handle_result(result)
    }

    #[post("member_update",tag = "更新会员信息",request_body = MemberUpdateReq)]
    async fn member_update(item: MemberUpdateReq) -> impl Responder {
        log::info!("member_update params: {:?}", &item);
        let member = InterviewMember {
            id: item.id,
            phone: item.phone,
            name: item.name,
            password: item.password,
            level: item.level,
            create_time: None,
            update_time: Some(DateTime::now()),
        };
        let result = InterviewMember::update_by_column(&mut RB.clone(), &member, "id").await;
        handle_result(result)
    }

    #[post("member_list",tag = "查询会员信息",request_body = MemberListReq)]
    async fn member_list(item: MemberListReq) -> impl Responder {
        log::info!("member_list params: {:?}", &item);
        let phone = item.phone.as_deref().unwrap_or_default();
        let name = item.name.as_deref().unwrap_or_default();
        let level = item.level.as_deref().unwrap_or_default();
        let page = &PageRequest::new(item.page_no, item.page_size);
        let result = InterviewMember::select_page_by_name(&mut RB.clone(), page, phone, name, level).await;
        let mut res = Response::default();
        match result {
            Ok(d) => {
                let mut member_list_data: Vec<MemberListData> = Vec::new();
                for x in d.records {
                    member_list_data.push(MemberListData {
                        id: x.id,
                        phone: x.phone,
                        name: x.name,
                        password: x.password,
                        level: x.level,
                        create_time: x.create_time.unwrap().0.to_string(),
                        update_time: x.update_time.unwrap().0.to_string(),
                    })
                }
                res.json(&ok_result_page(member_list_data, d.total));
                res
            }
            Err(err) => {
                log::error!("{}", err.to_string());
                res.json(&err_result_page(err.to_string()));
                res
            }
        }
    }
}
